//
//  ModifyPersonDataController.m
//  Youhuiju
//
//  Created by liuovyue on 16/8/15.
//  Copyright © 2016年 yutenys. All rights reserved.
//

#import "ModifyPersonDataController.h"
#import "DataController.h"

@interface ModifyPersonDataController ()<UITextFieldDelegate>

@property (nonatomic, strong)UILabel *labelTitle;
@property (nonatomic, strong)UIButton *buttonReturn;
@property (nonatomic, strong)UIView *viewBar;
@property (nonatomic, strong)UIButton *buttonSure;
@property (nonatomic, strong)UIView *viewBack;

@property (strong, nonatomic) IBOutlet UITextField *textFiledName;
//@property (nonatomic, assign) bool addName;


@end

@implementation ModifyPersonDataController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.view.backgroundColor = [UIColor colorWithRed:0.902 green:0.902 blue:0.902 alpha:1.0];
    
    
    
    [self createNavigationBar];
    
    
}


#pragma mark --顶部导航栏
- (void)createNavigationBar{
    
    self.viewBar = [[UIView alloc]initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 64)];
    self.viewBar.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.viewBar];
    
    
    
    self.labelTitle = [[UILabel alloc]init];
    self.labelTitle.text = @"修改昵称";
    self.labelTitle.textAlignment = NSTextAlignmentCenter;
    self.labelTitle.textColor = [UIColor blackColor];
    self.labelTitle.font = [UIFont systemFontOfSize:18*FitW];
    [self.viewBar addSubview:self.labelTitle];
    
    [_labelTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        
        //make.top.equalTo(self.viewBar.mas_top);
        make.left.equalTo(self.viewBar.mas_left).offset(100*FitW);
        make.right.equalTo(self.viewBar.mas_right).offset(-100*FitW);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-14*FitH);
        
        
        
    }];
    //返回按钮
    self.buttonReturn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.buttonReturn setImage:[UIImage imageNamed:@"zfanhuijian"] forState:UIControlStateNormal];
    [self.buttonReturn addTarget:self action:@selector(tuihuiAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonReturn];
    
    [_buttonReturn mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.equalTo(self.viewBar.mas_left).offset(17*FitW);
        make.width.mas_equalTo(22*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
        
        
    }];
    
    self.buttonSure = [UIButton buttonWithType:UIButtonTypeSystem];
    self.buttonSure.tintColor = [UIColor grayColor];
    [self.buttonSure setTitle:@"确定" forState: UIControlStateNormal];
    self.buttonSure.titleLabel.font = [UIFont systemFontOfSize:18*FitW];
    [self.buttonSure addTarget:self action:@selector(sureAcion:) forControlEvents:UIControlEventTouchUpInside];
    [self.viewBar addSubview:self.buttonSure];
    [self.buttonSure mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self.viewBar.mas_right).offset(-17*FitW);
        make.height.mas_equalTo(22*FitH);
        make.bottom.equalTo(self.viewBar.mas_bottom).offset(-13*FitH);
        
    }];
    
    
}
- (void)tuihuiAcion:(UIButton *)button {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)sureAcion:(UIButton *)button {


    
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression
                                  regularExpressionWithPattern:@"([^A-Za-z0-9-\u4e00-\u9fff])"
                                  options:0
                                  error:&error];

    NSString *resString = [regex stringByReplacingMatchesInString:self.textFiledName.text options:0 range:NSMakeRange(0, self.textFiledName.text.length) withTemplate:@""];
    
    if ([self.textFiledName.text isEqualToString:resString]&&self.textFiledName.text.length < 21&&self.textFiledName.text.length >3) {
         [self.navigationController popViewControllerAnimated:YES];
        
        [self.delegate passName:self.textFiledName];
        
    }else {
        
        self.viewBack = [[UIView alloc]init];
        self.viewBack.backgroundColor = [UIColor colorWithRed:0.7037 green:0.7037 blue:0.7037 alpha:0.375565732758621];
        [self.view addSubview:self.viewBack];
        [self.viewBack mas_makeConstraints:^(MASConstraintMaker *make) {
           
            make.top.equalTo(self.view.mas_top);
            make.bottom.equalTo(self.view.mas_bottom);
            make.right.equalTo(self.view.mas_right);
            make.left.equalTo(self.view.mas_left);
        }];
        
        [[[UIApplication sharedApplication] keyWindow] endEditing:YES];
        
        UIView *viewFormat = [[UIView alloc]init];
        viewFormat.backgroundColor = [UIColor whiteColor];
      
        [viewFormat.layer setCornerRadius:6.0];
        [self.viewBack addSubview:viewFormat];
        [viewFormat mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(self.view.mas_top).offset(250*FitH);
            make.bottom.equalTo(self.view.mas_bottom).offset(-275*FitH);
            make.right.equalTo(self.view.mas_right).offset(-60*FitW);
            make.left.equalTo(self.view.mas_left).offset(60*FitW);
        }];
        UILabel *labelFormat = [[UILabel alloc]init];
        labelFormat.text = @"昵称格式不正确";
        labelFormat.textAlignment = NSTextAlignmentCenter;
        labelFormat.font = [UIFont systemFontOfSize:24*FitW];
        [viewFormat addSubview:labelFormat];
        [labelFormat mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(viewFormat.mas_top).offset(40*FitH);
            
            make.right.equalTo(viewFormat.mas_right);
            make.left.equalTo(viewFormat.mas_left);
        }];
        UIButton *buttonFormat = [UIButton buttonWithType:UIButtonTypeSystem];
        [buttonFormat setTitle:@"我知道了" forState:UIControlStateNormal];
        buttonFormat.titleLabel.font = [UIFont systemFontOfSize:16*FitW];
        buttonFormat.tintColor = [UIColor colorWithHexString:@"#fd487f"];
        [buttonFormat addTarget:self action:@selector(knowAcion:) forControlEvents:UIControlEventTouchUpInside];
        [viewFormat addSubview:buttonFormat];
        
        
        [buttonFormat mas_makeConstraints:^(MASConstraintMaker *make) {
            
            make.top.equalTo(labelFormat.mas_top).offset(20*FitH);
            
            make.right.equalTo(viewFormat.mas_right).offset(-30*FitW);
            make.left.equalTo(viewFormat.mas_left).offset(30*FitW);
            make.bottom.equalTo(viewFormat.mas_bottom).offset(-10*FitH);
        }];
        
        
        
       
    }
    


    
}

- (void)knowAcion:(UIButton *)button {
    
    [self.viewBack removeFromSuperview];
    
}



//+ (BOOL) validateUserName:(NSString *)name
//{
//    NSString *userNameRegex = @"^[A-Za-z0-9]{6,20}+$";
//    NSPredicate *userNamePredicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",userNameRegex];
//    BOOL B = [userNamePredicate evaluateWithObject:name];
//    return B;
//}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
